<template>
  <view class="container">
    <web-view v-if="safeUrl" :src="safeUrl"></web-view>
    <view class="empty" v-else>
      <text class="empty-text">未找到可展示的链接</text>
      <button class="back-btn" @click="goBack">返回</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      safeUrl: ''
    }
  },
  onLoad(options) {
    const url = decodeURIComponent(options.url || '')
    if (url && this.validateUrl(url)) {
      this.safeUrl = url
    } else {
      uni.showToast({
        title: '无效的链接地址',
        icon: 'none'
      })
    }
  },
  methods: {
    validateUrl(url) {
      return /^https?:\/\//i.test(url)
    },
    goBack() {
      const pages = getCurrentPages()
      if (pages.length > 1) {
        uni.navigateBack()
      } else {
        uni.switchTab({
          url: '/pages/village-intro/village-intro'
        })
      }
    }
  }
}
</script>

<style scoped>
.container,
.empty {
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.empty-text {
  color: #999;
  font-size: 30rpx;
  margin-bottom: 20rpx;
}

.back-btn {
  width: 200rpx;
  background-color: #4caf50;
  color: #fff;
  border-radius: 40rpx;
}
</style>

